package com.szw.dlyk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szw.dlyk.constant.Constant;
import com.szw.dlyk.domain.Customer;
import com.szw.dlyk.manager.CustomerManager;
import com.szw.dlyk.query.CustomerQuery;
import com.szw.dlyk.result.CustomerExcel;
import com.szw.dlyk.service.CustomerService;
import com.szw.dlyk.mapper.CustomerMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author asus
* @description 针对表【t_customer(客户表)】的数据库操作Service实现
* @createDate 2024-01-21 18:16:51
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService{

    @Resource
    private CustomerManager customerManager;

    @Resource
    private CustomerMapper customerMapper;

    @Override
    public Boolean convertCustomer(CustomerQuery customerQuery) {
        return customerManager.convertCustomer(customerQuery);
    }

    @Override
    public PageInfo<Customer> getCustomerByPage(Integer current) {
        //1.设置PageHelper
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        //2.查询
        List<Customer> list = customerMapper.selectCustomerPage();
        //3.封装分页数据到PageInfo
        PageInfo<Customer> info = new PageInfo<>(list);

        return info;
    }

    @Override
    public List<CustomerExcel> getCustomerByExcel(List<String> idList) {
        List<CustomerExcel> customerExcelList = new ArrayList<>();

        List<Customer> customerList = customerMapper.selectCustomerByExcel(idList);

        //把从数据库查询出来的List<TCustomer>数据，转换为 List<CustomerExcel>数据
        customerList.forEach(customer -> {
            CustomerExcel customerExcel = new CustomerExcel();

            //需要一个一个设置，没有办法，因为没法使用BeanUtils复制
            customerExcel.setOwnerName(ObjectUtils.isEmpty(customer.getOwnerDO()) ? Constant.EMPTY : customer.getOwnerDO().getName());
            customerExcel.setActivityName(ObjectUtils.isEmpty(customer.getActivityDO()) ? Constant.EMPTY : customer.getActivityDO().getName());
            customerExcel.setFullName(customer.getClueDO().getFullName());
            customerExcel.setAppellationName(ObjectUtils.isEmpty(customer.getAppellationDO()) ? Constant.EMPTY : customer.getAppellationDO().getTypeValue());
            customerExcel.setPhone(customer.getClueDO().getPhone());
            customerExcel.setWeixin(customer.getClueDO().getWeixin());
            customerExcel.setQq(customer.getClueDO().getQq());
            customerExcel.setEmail(customer.getClueDO().getEmail());
            customerExcel.setAge(customer.getClueDO().getAge());
            customerExcel.setJob(customer.getClueDO().getJob());
            customerExcel.setYearIncome(customer.getClueDO().getYearIncome());
            customerExcel.setAddress(customer.getClueDO().getAddress());
            customerExcel.setNeedLoadName(ObjectUtils.isEmpty(customer.getNeedLoanDO()) ? Constant.EMPTY : customer.getNeedLoanDO().getTypeValue());
            customerExcel.setProductName(ObjectUtils.isEmpty(customer.getIntentionProductDO()) ? Constant.EMPTY : customer.getIntentionProductDO().getName());
            customerExcel.setSourceName(ObjectUtils.isEmpty(customer.getSourceDO()) ? Constant.EMPTY : customer.getSourceDO().getTypeValue());
            customerExcel.setDescription(customer.getDescription());
            customerExcel.setNextContactTime(customer.getNextContactTime());

            customerExcelList.add(customerExcel);
        });
        return customerExcelList;
    }
}




